package com.example.my_rabbitmq_high_demo.confirm;

import com.rabbitmq.client.Channel;
import com.rabbitmq.client.ConfirmListener;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;

import java.io.IOException;
import java.util.concurrent.TimeoutException;

/**
 * @Description: confirm确认消息模式生产者
 * @Author: GaoJian
 * @Date: 2022/7/6 21:34
 */
public class ProducerByConfirm {
    public static void main(String[] args) throws IOException, TimeoutException {
        //1、创建ConnectionFactory，并设置属性
        ConnectionFactory factory = new ConnectionFactory();
        factory.setHost("127.0.0.1");
        factory.setPort(5672);
        factory.setVirtualHost("/");

        //2、创建连接
        Connection connection = factory.newConnection();

        //3、创建channel
        Channel channel = connection.createChannel();

        //4、开启Confirm模式
        channel.confirmSelect();

        String exchangeName = "test_confirm_exchange";
        String routingKey = "confirm.test";

        //5、发送消息
        String msg = "Send Msg By Confirm...";
        channel.basicPublish(exchangeName, routingKey, null, msg.getBytes());

        //6、设置监听
        channel.addConfirmListener(new ConfirmListener() {
            @Override
            public void handleAck(long deliveryTag, boolean multiple) throws IOException {
                System.out.println("-----ACK Success-----");
            }

            @Override
            public void handleNack(long deliveryTag, boolean multiple) throws IOException {
                System.out.println("-----ACK Failed-----");
            }
        });
        //因为设置了监听，这里就不关闭channel和connection了
    }
}
